package com.ss.android.article.base.feature.f;

import android.net.Uri;
import android.os.Message;
import android.view.SurfaceHolder;

import com.bytedance.article.common.c.TTUtils;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.app.BizApplication;
import com.ss.android.m.c.a.a;
import com.ss.android.m.c.a.b;
import com.ss.android.newmedia.BaseApplication;

import tv.danmaku.ijk.media.player.AndroidMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnBufferingUpdateListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnCompletionListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnErrorListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnInfoListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnPreparedListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnSeekCompleteListener;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public final class e implements a, OnBufferingUpdateListener, OnCompletionListener, OnErrorListener, OnInfoListener, OnPreparedListener, OnSeekCompleteListener {
    private static int b;
    private static int c;

    static {
        e.b = 0;
        e.c = 0;
    }

    private IMediaPlayer a;
    private b d;

    public e() {
        this.a = null;
        e.b = 0;
        if (!AppData.getInstance().an() && e.c < 3) {
            IjkMediaPlayer.loadLibrariesOnce(BaseApplication.getApplication());
            if (!IjkMediaPlayer.isLibLoaded()) {
            } else {
                e.b = 1;
                try {
                    IjkMediaPlayer.setIPV6First(AppData.getInstance().videoPlayUseIpV6());
                } catch (Throwable v0) {
                }
            }
        }
        this.l();
    }

    public static void k() {
        if (e.b == 1) {
            ++e.c;
        }
    }

    public final void a() {
        if (this.a != null) {
            this.a.start();
        }
    }

    public final void a(long arg2) {
        if (this.a != null) {
            this.a.seekTo(arg2);
        }
    }

    public final void a(SurfaceHolder arg3) {
        this.l();
        this.a.setDisplay(arg3);
        this.a.setScreenOnWhilePlaying(true);
    }

    public final void a(b arg1) {
        this.d = arg1;
    }

    public final void a(String arg4) throws Exception {
        this.l();
        if (TTUtils.isValidUrl(arg4)) {
            this.a.setDataSource(arg4);
        } else {
            this.a.setDataSource(BizApplication.getApplication(), Uri.parse(arg4));
        }
    }

    public final void b() {
        if (this.a != null && (this.a.isPlaying())) {
            this.a.pause();
        }
    }

    public final void c() {
        if (this.a != null) {
            this.a.reset();
        }
    }

    public final void d() {
        if (this.a != null) {
            this.a.release();
            this.a = null;
        }
    }

    public final void e() throws Exception {
        if (this.a != null) {
            if (e.b == 0) {
                ((AndroidMediaPlayer)this.a).getInternalMediaPlayer().prepare();
            } else {
                this.a.prepareAsync();
            }
        }
    }

    public final void f() {
        if (this.a != null) {
            this.a.stop();
        }
    }

    public final long g() {
        long v0 = this.a == null ? 0 : this.a.getDuration();
        return v0;
    }

    public final long h() {
        long v0 = this.a == null ? 0 : this.a.getCurrentPosition();
        return v0;
    }

    public final int i() {
        int v0 = this.a == null ? -1 : e.b;
        return v0;
    }

    public final boolean j() {
        boolean v0 = this.a == null ? false : this.a.isPlaying();
        return v0;
    }

    private void l() {
        if (this.a == null) {
            this.a = e.b == 1 ? new IjkMediaPlayer(BaseApplication.getApplication()) : new AndroidMediaPlayer();
            this.a.setOnPreparedListener(((OnPreparedListener) this));
            this.a.setOnCompletionListener(((OnCompletionListener) this));
            this.a.setOnErrorListener(((OnErrorListener) this));
            this.a.setOnBufferingUpdateListener(((OnBufferingUpdateListener) this));
            this.a.setOnSeekCompleteListener(((OnSeekCompleteListener) this));
            this.a.setOnInfoListener(((OnInfoListener) this));
            this.a.setLooping(false);
        }
    }

    public final void onBufferingUpdate(IMediaPlayer arg5, int arg6) {
        if (this.a == arg5 && this.d != null) {
            com.ss.android.m.c.a.b v0 = this.d;
            long v2 = ((long) arg6);
            if (v0.a.d != null) {
                v0.a.d.obtainMessage(312, Long.valueOf(v2)).sendToTarget();
            }
        }
    }

    public final void onCompletion(IMediaPlayer arg3) {
        if (e.b == 1) {
            e.c = 0;
        }

        if (this.d != null) {
            b v0 = this.d;
            if (v0.a.d != null) {
                v0.a.d.obtainMessage(310).sendToTarget();
            }
        }
    }

    public final boolean onError(IMediaPlayer arg3, int arg4, int arg5) {
        e.k();
        if (this.d != null) {
            b v0 = this.d;
            if (v0.a.d != null) {
                Message v0_1 = v0.a.d.obtainMessage(400);
                v0_1.arg1 = arg4;
                v0_1.arg2 = arg5;
                v0_1.sendToTarget();
            }
        }

        return false;
    }

    public final boolean onInfo(IMediaPlayer arg4, int arg5, int arg6) {
        if (this.a == arg4 && this.d != null) {
            b v0 = this.d;
            if (v0.a.d != null) {
                Message v0_1 = v0.a.d.obtainMessage(311);
                v0_1.arg1 = arg5;
                v0_1.arg2 = arg6;
                v0_1.sendToTarget();
            }
        }

        return false;
    }

    public final void onPrepared(IMediaPlayer arg3) {
        if (e.b == 1) {
            e.c = 0;
        }

        if (this.d != null) {
            b v0 = this.d;
            if (v0.a.d != null) {
                v0.a.d.obtainMessage(304).sendToTarget();
            }
        }
    }

    public final void onSeekComplete(IMediaPlayer arg3) {
        if (this.d != null) {
            b v0 = this.d;
            if (v0.a.d != null) {
                v0.a.d.obtainMessage(308).sendToTarget();
            }
        }
    }
}

